﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using FunstreamTV.Model;
using FunstreamTV.Model.Category;
using FunstreamTV.Model.Content;
using FunstreamTV.Model.Stream;
using FunstreamTV.Model.Subscribe;
using FunstreamTV.Tools;
using FunstreamTV.ViewModel.ObjectViewModel;
using Category = FunstreamTV.Model.Category.Category;

namespace FunstreamTV.ViewModel
{
    public class ClientViewModel : BaseViewModel
    {
        private List<Category> categories;
        private Visibility categoriesVisibility;
        private Visibility groupedStreamsVisibility;
        private List<ContentViewModel> groupedStreams;
        private Visibility goBackAppButtonVisibility;
        public ObservableCollection<ContentViewModel> TopStreams { get; set; }
        public ObservableCollection<ContentViewModel> FavoritesStreams { get; set; }

        public List<ContentViewModel> GroupedStreams
        {
            get { return groupedStreams; }
            set { groupedStreams = value; OnPropertyChanged(); }
        }

        public List<Category> Categories
        {
            get { return categories; }
            set { categories = value; OnPropertyChanged(); }
        }

        public Visibility CategoriesVisibility
        {
            get { return categoriesVisibility; }
            set { categoriesVisibility = value; OnPropertyChanged(); }
        }

        public Visibility GroupedStreamsVisibility
        {
            get { return groupedStreamsVisibility; }
            set { groupedStreamsVisibility = value; OnPropertyChanged(); }
        }

        public Visibility GoBackAppButtonVisibility
        {
            get { return goBackAppButtonVisibility; }
            set { goBackAppButtonVisibility = value; OnPropertyChanged(); }
        }

        public ClientViewModel()
        {
            TopStreams = new ObservableCollection<ContentViewModel>();
            FavoritesStreams = new ObservableCollection<ContentViewModel>();
            GroupedStreamsVisibility = Visibility.Collapsed;
            CategoriesVisibility = Visibility.Visible;
            GoBackAppButtonVisibility = Visibility.Collapsed;
        }

        public async Task Load()
        {
            Requester requester;
            lock (this)
            {
                requester = new Requester();
            }

            Response<List<StreamResponse>> response = await requester.Post<List<StreamResponse>, Top>("/content/top", new Top { amount = 500, content = "stream" });
            if (response.Item == null)
            {
                await new MessageDialog(response.Message).ShowAsync();
                return;
            }

            TopStreams.Clear();
            foreach (var content in response.Item)
                TopStreams.Add(new ContentViewModel
                {
                    adult = content.adult,
                    category = content.category,
                    description = content.description,
                    image = content.image,
                    id = content.id,
                    name = content.name,
                    players = content.players,
                    rating = content.rating,
                    streamer = content.streamer,
                    start_at = content.start_at,
                    thumbnail = content.thumbnail
                });
            var categoryResponse = await requester.Post<CategoryResponse, Category>("/category", new Category { content = "stream", slug = "top", options = new CategoryOptions { subCategories = true } });

            Categories = categoryResponse.Item.subCategories;
            response = await requester.Post<List<StreamResponse>, List>("/subscribe/list", new List { content = "stream" });
            if (response.Item == null)
            {
                await new MessageDialog(response.Message).ShowAsync();
            }

            FavoritesStreams.Clear();
            foreach (var content in response.Item)
                FavoritesStreams.Add(new ContentViewModel
                {
                    adult = content.adult,
                    category = content.category,
                    description = content.description,
                    image = content.image,
                    id = content.id,
                    name = content.name,
                    players = content.players,
                    rating = content.rating,
                    streamer = content.streamer,
                    start_at = content.start_at,
                    thumbnail = content.thumbnail
                });
        }

        public async Task LoadSubcategory(int categoryID)
        {
            var requester = new Requester();
            var categoryResponse = await requester.Post<CategoryResponse, Category>("/category",
                categoryID != 0
                    ? new Category
                    {
                        content = "stream",
                        id = categoryID,
                        options = new CategoryOptions {subCategories = true}
                    }
                    : new Category
                    {
                        content = "stream",
                        slug = "top",
                        options = new CategoryOptions {subCategories = true}
                    }
                );
            if (categoryResponse.Item?.subCategories.Count != 0)
            {
                CategoriesVisibility = Visibility.Visible;
                GroupedStreamsVisibility = Visibility.Collapsed;
                Categories = categoryResponse.Item.subCategories;
            }
            else
            {
                CategoriesVisibility = Visibility.Collapsed;
                GroupedStreamsVisibility = Visibility.Visible;
                var response = await requester.Post<List<StreamResponse>, Top>("/content/top", new Top { amount = 666, category = new ContentCategory { id = categoryResponse.Item.id.Value }, content = "stream" });
                if (response.Item != null)
                    GroupedStreams = response.Item.Where(i => i.category.id.HasValue && i.category.id.Value.Equals(categoryResponse.Item.id.Value)).Select(content => new ContentViewModel
                    {

                        adult = content.adult,
                        category = content.category,
                        description = content.description,
                        image = content.image,
                        id = content.id,
                        name = content.name,
                        players = content.players,
                        rating = content.rating,
                        streamer = content.streamer,
                        start_at = content.start_at,
                        thumbnail = content.thumbnail
                    }).ToList();
            }
        }
    }
}
